Skip to content

mcp: reject relative paths in tool handlers#3694

Open
Shrey327 wants to merge 2 commits into
knative:mainfrom
Shrey327:mcp-validate-absolute-path
Open

mcp: reject relative paths in tool handlers#3694
Shrey327 wants to merge 2 commits into
knative:mainfrom
Shrey327:mcp-validate-absolute-path

Conversation

@Shrey327
Copy link
Copy Markdown

Description

Adds validatePath() to the MCP server that rejects non-absolute path arguments before shelling out to the func CLI. Since cmd.Dir is never set in the executor, relative paths silently resolved against the MCP server's working directory instead of the user's project turning a wrong directory failure into a silent misbehavior. This change turns that into an actionable error, matching the guidance already documented in instructions.md.All existing tests updated to use t.TempDir() and a TestValidatePath unit test added.

…ed tests

Signed-off-by: Shrey327 <shreyansh.pathak273@gmail.com>
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 10, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Shrey327
Once this PR has been reviewed and has the lgtm label, please assign dsimansk for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 10, 2026 14:24
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 10, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@knative-prow knative-prow Bot added the size/L 🤖 PR changes 100-499 lines, ignoring generated files. label May 10, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 10, 2026

Welcome @Shrey327! It looks like this is your first PR to knative/func 🎉

@knative-prow knative-prow Bot added the needs-ok-to-test 🤖 Needs an org member to approve testing label May 10, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 10, 2026

Hi @Shrey327. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link
Copy Markdown
Member

@lkingland lkingland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Just two small suggestions.

Comment thread pkg/mcp/tools.go
func validatePath(path string) error {
if !filepath.IsAbs(path) {
return fmt.Errorf("path must be absolute, got %q", path)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a more agent-friendly error message — the client is usually an LLM. Something like:

return fmt.Errorf("path must be absolute (the MCP server's working directory is not the user's project root), got %q", path)

The parenthetical hints at the why, which helps the agent fix its call instead of just retrying with a different relative path.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review i have made the changes

Comment thread pkg/mcp/tools_test.go
}
if err := validatePath("."); err == nil {
t.Fatal("expected error for '.', got nil")
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding handler-level rejection tests so the wiring is covered, not just the helper. Something like:

func TestTool_Deploy_RejectsRelativePath(t *testing.T) {
    client, _, err := newTestPair(t)
    if err != nil { t.Fatal(err) }
    result, err := client.CallTool(t.Context(), &mcp.CallToolParams{
        Name:      "deploy",
        Arguments: map[string]any{"path": "."},
    })
    if err != nil { t.Fatal(err) }
    if !result.IsError { t.Fatal("expected error for relative path") }
}

One per handler is verbose but mechanical. Alternatively, a table-driven test parameterized over tool names would cover all of them in one place.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the changes , i used the table-driven approach

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.01%. Comparing base (0209813) to head (766fda2).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3694      +/-   ##
==========================================
+ Coverage   56.90%   57.01%   +0.10%     
==========================================
  Files         181      181              
  Lines       20933    20973      +40     
==========================================
+ Hits        11912    11957      +45     
+ Misses       7811     7808       -3     
+ Partials     1210     1208       -2     
Flag Coverage Δ
e2e 36.08% <0.00%> (-0.08%) ⬇️
e2e go 32.68% <0.00%> (-0.07%) ⬇️
e2e node 28.42% <0.00%> (-0.06%) ⬇️
e2e python 33.05% <0.00%> (-0.07%) ⬇️
e2e quarkus 28.56% <0.00%> (-0.06%) ⬇️
e2e rust 27.97% <0.00%> (-0.05%) ⬇️
e2e springboot 26.47% <0.00%> (-0.04%) ⬇️
e2e typescript 28.53% <0.00%> (-0.08%) ⬇️
e2e-config-ci 17.84% <0.00%> (-0.04%) ⬇️
integration 17.42% <0.00%> (-0.01%) ⬇️
unit macos-14 45.09% <100.00%> (+0.10%) ⬆️
unit macos-latest 45.09% <100.00%> (+0.10%) ⬆️
unit ubuntu-24.04-arm 45.28% <100.00%> (+0.11%) ⬆️
unit ubuntu-latest 45.94% <100.00%> (+0.10%) ⬆️
unit windows-latest 45.08% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Shrey327 <shreyansh.pathak273@gmail.com>
@Shrey327 Shrey327 requested a review from lkingland May 11, 2026 13:16
@gauron99
Copy link
Copy Markdown
Contributor

/ok-to-test

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants